<?php
// +----------------------------------------------------------------------
// | Yzncms [ 御宅男工作室 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2018 http://yzncms.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 御宅男 <530765310@qq.com>
// +----------------------------------------------------------------------

// +----------------------------------------------------------------------
// | 后台用户管理
// +----------------------------------------------------------------------
namespace app\common\model;

use app\admin\controller\Staff;
use app\admin\model\AdminUser;
use app\admin\model\CarModel;
use app\admin\model\College;
use app\api\model\Token;
use app\common\model\AliPay;
use app\common\model\RebateLog;
use EasyWeChat\Factory;
use think\Exception;
use think\facade\Db;
use think\Model;


class ApplyTeamModel extends Model
{
    protected $autoWriteTimestamp = true;

    protected $table = 'yzn_apply_team';


    public function getApplyTeamInfo($prams = array())
    {
        $team = (new ApplyTeamModel())->where(['uid' => $prams['uid']])->field('id,aid,status,no_reason,add_reason')->find();
        if (!$team) {
            return ['code' => 201, 'msg' => '您还没有加入团队'];
        }
        //获取团队信息
        $admin = (new AdminUser())->where(['id' => $team['aid']])->find();
        $team['team_name'] = $admin['team_name'];
        $team['nickname'] = $admin['nickname'];

        return ['code' => 200, 'msg' => '获取成功', 'data' => $team];
    }


    public function addTeam($params)
    {
        if (!$params['aid'] || !$params['add_reason']) {
            return ['code' => 201, 'msg' => '缺少参数'];
        }

        //获取信息
        $model = (new ApplyTeamModel())->where(['uid' => $params['uid']])->find();
        if (!$model) {
            $model = (new ApplyTeamModel());
            $model->uid = $params['uid'];
        }
        $model->aid = $params['aid'];
        $model->status = 0;
        $model->no_reason = NULL;
        $model->add_reason = $params['add_reason'];

        if (!$model->save()) {
            return ['code' => 201, 'msg' => '操作失败，请重新尝试'];
        }

        return ['code' => 200, 'msg' => '申请成功'];

    }


    public function getCount($params, $aid)
    {
        //获取待审核团队数量
        $count = (new ApplyTeamModel())->where(['aid' => $aid, 'status' => $params['status']])->count();
        return ['code' => 200, 'msg' => 'success', 'data' => $count];
    }

    public function getTeamList($params)
    {
        $status = isset($params['status']) ? $params['status'] : 1;
        $map[] = ['t.status', '=', $status];
        if (isset($params['title']) && $params['title']) {
            $map[] = ['s.username', 'like', "%" . $params['title'] . "%"];
        }
        $base_model = (new ApplyTeamModel())->alias('t')
            ->leftJoin('staff s', 's.id = t.uid')
            ->where($map);

        $list = $base_model->field('t.id,s.username,s.avatar,t.add_reason,t.create_time,t.status,t.no_reason')
            ->paginate($params['limit'], false, ['page' => $params['page']]);

        return ['code' => 200, 'msg' => '申请成功', 'data' => $list];

    }

    public function refuseTeam($params = array(), $aid)
    {
        if (!isset($params['no_reason']) || !$params['no_reason']) {
            return ['code' => 201, 'msg' => '缺少参数'];
        }
        $applyTeam = (new ApplyTeamModel())->where(['id' => $params['id'], 'status' => 0])->find();
        if (!$applyTeam) {
            return ['code' => 201, 'msg' => '数据有误'];
        }
        $applyTeam->no_reason = $params['no_reason'];
        $applyTeam->status = 2;
        if (!$applyTeam->save()) {
            return ['code' => 201, 'msg' => '操作失败'];
        }
        return ['code' => 200, 'msg' => '操作成功'];
    }


    public function agreeTeam($params = array(), $aid)
    {
        if (!isset($params['id']) || !$params['id']) {
            return ['code' => 201, 'msg' => '缺少参数'];
        }
        $applyTeam = (new ApplyTeamModel())->where(['id' => $params['id'], 'status' => 0])->find();
        if (!$applyTeam) {
            return ['code' => 201, 'msg' => '数据有误'];
        }
        $applyTeam->status = 1;
        $applyTeam->no_reason = NULL;
        if (!$applyTeam->save()) {
            return ['code' => 201, 'msg' => '操作失败'];
        }
        //更改用户的信息
        (new StaffModel())->where(['id' => $applyTeam['uid']])->update(['aid' => $applyTeam['aid']]);
        return ['code' => 200, 'msg' => '操作成功'];
    }

    public function updatePassword($params)
    {
        if (!isset($params['id']) || !$params['id']) {
            return ['code' => 201, 'msg' => '缺少参数'];
        }
        $applyTeam = (new ApplyTeamModel())->where(['id' => $params['id']])->find();

        if (!isset($params['newPassword']) || !$params['newPassword']) {
            return ['code' => 201, 'msg' => '密码不能为空'];
        }


        if (trim($params['newPassword']) != trim($params['password_confirm'])) {
            return ['code' => 201, 'msg' => '两次密码输入不一致'];
        }

        $passwordinfo = encrypt_password(trim($params['newPassword'])); //对密码进行处理
        $data['password'] = $passwordinfo['password'];
        $data['encrypt'] = $passwordinfo['encrypt'];

        if (!(new StaffModel())->where(['id' => $applyTeam['uid']])->update($data)) {
            return ['code' => 201, 'msg' => '修改失败'];
        }
        return ['code' => 200, 'msg' => '修改成功'];
    }

    public function removeStaff($params)
    {
        if (!isset($params['id']) || !$params['id']) {
            return ['code' => 201, 'msg' => '缺少参数'];
        }
        $applyTeam = (new ApplyTeamModel())->where(['id' => $params['id']])->find();
        if (!$applyTeam) {
            return ['code' => 201, 'msg' => '数据有误'];
        }

        if (!$applyTeam->delete()) {
            return ['code' => 201, 'msg' => '操作失败'];
        }
        //修改用户aid
        (new StaffModel())->where(['id' => $applyTeam['uid']])->update(['aid' => 0]);

        return ['code' => 200, 'msg' => '操作成功'];


    }
}
